package bookpos.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import bookpos.entity.Inventory;
import bookpos.entity.Orders;
import bookpos.service.InventoryService;
import bookpos.service.OrderItemService;
import bookpos.service.OrdersService;
import bookpos.util.core.action.BaseAction;
import bookpos.util.core.action.Json;
import bookpos.util.core.util.ExceptionUtil;

import com.opensymphony.xwork2.ModelDriven;
/**
 * 库存控制器 InventoryAction
 * @author WZB
 *
 */
@Controller
@Action(value = "sale", results = { @Result(name = "sale", location = "/page/sale/sale.jsp") })
public class SaleAction extends BaseAction implements ModelDriven<Orders> {
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(SaleAction.class);
	@Autowired
	private OrdersService service;
	@Autowired
	private OrderItemService itemService;
	@Autowired
	private InventoryService inventoryService;
	
	private String oid;
	private String ids;//以 ","分割商品id
	private String qs;//以 ","分割商品数量
	private String prices;//以 ","分割商品价格
	private String totals;//以 ","分割商品
	private String total;
	
	
	/**
	 * 销售项目存储方法
	 */
	public void saveItems() {
			Json result = new Json();
			try {
				System.out.println("test1");
				itemService.saveItems(ids.split(","), qs.split(","),prices.split(","),totals.split(","),getOid());
				System.out.println("test1111");
				result.setSuccess(true);
				result.setMsg("订单项目保存成功");
				result.setSuccess(true);
				saleGoods();
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("订单项目失败");
			}
			writeJson(result);
			
	}
	/**
	 * 销售项目存储方法
	 */
	public void saveOrders() {
			Json result = new Json();
			try {
				service.saveOrders(oid, getTotal());
				result.setSuccess(true);
				result.setMsg("订单保存成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("订单保存失败");
			}
			writeJson(result);
	}
	
	/**
	 * 销售库存操作方法
	 */
	public void saleGoods() {
			Json result = new Json();
			try {
				inventoryService.saleGoods(ids.split(","), qs.split(","));
				result.setSuccess(true);
				result.setMsg("减少库存成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("减少库存失败");
			}
			writeJson(result);
	}


	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getQs() {
		return qs;
	}
	public void setQs(String qs) {
		this.qs = qs;
	}
	public String getPrices() {
		return prices;
	}
	public void setPrices(String prices) {
		this.prices = prices;
	}
	public String getTotals() {
		return totals;
	}
	public void setTotals(String totals) {
		this.totals = totals;
	}
	public String getOid() {
		return oid;
	}
	public void setOid(String oid) {
		this.oid = oid;
	}


	@Override
	public Orders getModel() {
		// TODO Auto-generated method stub
		return null;
	}
	public String getTotal() {
		return total;
	}
	public void setTotal(String total) {
		this.total = total;
	}	
	
}
